package com.commerce.reader.ui.notifications;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.commerce.reader.Activity.DescActivity;
import com.commerce.reader.Activity.ReadActivity;
import com.commerce.reader.Entity.Book;
import com.commerce.reader.R;
import com.commerce.reader.adapter.MyCollectionAdapter;
import com.commerce.reader.adapter.MycardAdapter;

import java.util.List;

public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;
    private MyCollectionAdapter myCollectionAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_notifications, container, false);
        final ListView listView = root.findViewById(R.id.fn_lv_book);
        notificationsViewModel.getList().observe(getViewLifecycleOwner(), new Observer<List<Book>>() {
            @Override
            public void onChanged(List<Book> books) {

//                myCollectionAdapter.notifyDataSetChanged();

            }
        });
        if(myCollectionAdapter==null){
            List<Book> book = notificationsViewModel.getList().getValue();
            myCollectionAdapter = new MyCollectionAdapter(getActivity(), book);
            listView.setAdapter(myCollectionAdapter);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                List<Book> book = notificationsViewModel.getList().getValue();
                Intent intent = new Intent(getActivity(), ReadActivity.class);
                intent.putExtra("ids",book.get(i).getIds());
                intent.putExtra("book",book.get(i).getBook());
                intent.putExtra("name",book.get(i).getName());
                intent.putExtra("bookLitepal", book.get(i));
                startActivity(intent);
            }
        });
        return root;
    }
}